#include "musicdataprocessor.h"
#include <qdebug.h>
#include <qurl.h>
#include <qjsondocument.h>
#include <qjsonarray.h>
#include <qjsonobject.h>
#include <qjsonvalue.h>
#include <qpixmap.h>
#include <qfile.h>

const QString MusicDataProcessor::GITEE_RESOURCE = "https://gitee.com/MarkYangUp/music/raw/master/";


MusicDataProcessor::MusicDataProcessor(QObject *parent)
	: QObject(parent)
{}

MusicDataProcessor::~MusicDataProcessor()
{}

void MusicDataProcessor::httpGetJsonRequest(const QString & musicInfo)
{
	QString	musicInfoUrl = QUrl::toPercentEncoding(musicInfo.toUtf8());
	m_baseResearchURL = GITEE_RESOURCE + musicInfoUrl;
	QString jsonUrl = m_baseResearchURL + "/music.json/";
	//qDebug() << jsonUrl;
	//qDebug() << QUrl(jsonUrl).toString();
	m_pHttpManager = new KHttpManager(this);
	connect(m_pHttpManager, &KHttpManager::readyReadHttp, this, &MusicDataProcessor::onGetHttpFinished);
	
	m_pHttpManager->httpGet(jsonUrl);
	
}

void MusicDataProcessor::httpGetImgRequest(const QString& imgURL)
{
	m_pHttpManager = new KHttpManager(this);
	connect(m_pHttpManager, &KHttpManager::readyReadHttp, this, &MusicDataProcessor::onGetHttpImgFinished);
	m_pHttpManager->httpGet(imgURL);
}

void MusicDataProcessor::httpGetLyricRequest(const QString& lryicURL)
{

	m_pHttpManager = new KHttpManager(this);
	connect(m_pHttpManager, &KHttpManager::readyReadHttp, this, &MusicDataProcessor::onGetHttpLyricFinished);
	m_pHttpManager->httpGet(lryicURL);
}

QVector<KSongWebInfo*> MusicDataProcessor::processAllSongsData(const QByteArray& data)
{
	QVector<KSongWebInfo*> songs;
	if (data.isEmpty())
		return songs;

	QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
	QJsonObject jsonObject = jsonDoc.object();
	QJsonArray jsonSongList = jsonObject["list"].toArray();
	for (const auto& ele : jsonSongList) 
	{
		QJsonObject object = ele.toObject();
		KSongWebInfo* pSong = new KSongWebInfo();
		QString path = object["path"].toString();
		QString singerName = path.split('/').back();
		singerName = QUrl::toPercentEncoding(singerName.toUtf8());
		QString resourceRootURL = m_baseResearchURL + '/' + singerName + "/";


		pSong->setMusicName(object["musicName"].toString());
		pSong->setAlbumName(object["albumName"].toString());
		pSong->setDuration(object["duration"].toInt());
		pSong->setMp3URLStr(resourceRootURL + object["mp3"].toString());
		pSong->setLyricURLStr(resourceRootURL + object["lyric"].toString());
		pSong->setCoverImgURLStr(resourceRootURL + object["img"].toString());
		songs.push_back(pSong);
	}
	return songs;
}

void MusicDataProcessor::onGetHttpImgFinished(QByteArray& httpData, QUrl requestURL)
{
	QPixmap* pixmap = new QPixmap;
	pixmap->loadFromData(httpData);
	emit readyReadImg(pixmap);

}

void MusicDataProcessor::onGetHttpLyricFinished(QByteArray& httpData, QUrl requestURL)
{
	QString* lyric = new QString(httpData);
	emit readyReadLyric(lyric);
}

void MusicDataProcessor::onGetHttpFinished(QByteArray& httpData, QUrl requestURL)
{
	QString urlString = requestURL.toString();
	qDebug() << "urlString:" << urlString;
	if (urlString.contains("/music.json/")) 
	{
		QVector<KSongWebInfo*> allSongs = processAllSongsData(httpData);
		emit readyReadAllSongs(allSongs);
	}
}

QString MusicDataProcessor::DecodeURL(const QString& u)
{
	QByteArray decode = QByteArray::fromPercentEncoding(u.toLocal8Bit());
	return QString::fromUtf8(decode);
}

QString MusicDataProcessor::EncodeURL(const QString& u)
{
	QByteArray encode = u.toLocal8Bit();
	
	encode = encode.toPercentEncoding(":/?=");
	return QString(encode);
}
